package com.zhouchb.test.singleton;

import com.sun.media.sound.RIFFInvalidDataException;
import javafx.scene.shape.VLineTo;

import javax.naming.event.NamingListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zhouchb
 * @Create 2022-02-2022/2/15 :22:20
 * @Description
 */
public class tree<Key extends Comparable, Value> {
    private Node root;
    private Integer num = 0;

    private class Node {
        private Key key;
        private Value value;
        private Node rightNode;
        private Node leftNode;

        public Node(Key key, Value value, Node rightNode, Node leftNode) {
            this.key = key;
            this.value = value;
            this.rightNode = rightNode;
            this.leftNode = leftNode;
        }
    }

    public void put(Key key, Value value) {
        root = put(root, key, value);
    }

    public Node put(Node x, Key key, Value value) {
        if (x == null) {
            x = new Node(key, value, null, null);
            return x;
        }
        int cmp = x.key.compareTo(key);
        if (cmp > 0) {
            x.rightNode = put(x.rightNode, key, value);
        }
        if (cmp < 0) {
            x.leftNode = put(x.leftNode, key, value);
        }
        if (cmp == 0) {
            x.value = value;
        }
        return x;
    }

    public List preSort() {
        List list = new ArrayList();
        preSort(list, root);
        return list;
    }

    public void preSort(List list, Node x) {
        if (x == null) {
            return;
        }
        list.add(x.key);
        preSort(list, x.leftNode);
        preSort(list, x.rightNode);
    }
}

class hah {
    public static void main(String[] args) {
        tree<String, String> s = new tree<>();
        s.put("a", "a");
        s.put("c", "a");
        s.put("b", "a");
        s.put("d", "a");
        s.put("e", "a");
        for (Object o : s.preSort()) {
            System.out.println(o);
        }

    }
}

class hehe {
    public static void main(String[] args) {
        int i = 0, j;
        for (i = 0, j = 0; i + j < 20; ++i, j += i--) {
            System.out.println(i + "==" + j);
            System.out.println(i + j);
            System.out.println("第" + j + "次");
        }
    }
}

class A {
    public int method(int a, int b) {
        return 0;
    }
}
class b extends A{
    public int method(int a, int b) {
        return 0;
    }
}